﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Common;
using NanoERP.Business.BaseInfo;

namespace NanoERP.BaseInfo
{
    //[Module("FrmStorageinfoEdit")]
    public partial class FrmStorageinfoEdit : ModuleForm
    {
        Storageinfo _storageinfo;

        readonly StorageinfoBLL _storageinfoBLL;
        readonly SysdictableBLL _sysdictablebLl;
        public FrmStorageinfoEdit()
        {
            InitializeComponent();
        }

        public FrmStorageinfoEdit(Sysdictable v)
        {
            InitializeComponent();

            _storageinfoBLL = new StorageinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _storageinfo = new Storageinfo { Stotype = v.Id };

            BindFormContro();
        }

        public FrmStorageinfoEdit(Storageinfo obj, Sysdictable v)
        {
            InitializeComponent();

            _storageinfoBLL = new StorageinfoBLL();
            _sysdictablebLl = new SysdictableBLL();
            _storageinfo = obj;

            BindFormContro();
        }

        /// <summary>
        /// 绑定控件值
        /// </summary>
        void BindFormContro()
        {
            cboStotype.DataSource = _sysdictablebLl.Query((int)SysDicEnum.仓库类别);

            txtStoname.DataBindings.Add(new Binding("Text", _storageinfo, "Stoname", true, DataSourceUpdateMode.OnPropertyChanged));
            txtStopym.DataBindings.Add(new Binding("Text", _storageinfo, "Stopym", true, DataSourceUpdateMode.OnPropertyChanged));
            txtStono.DataBindings.Add(new Binding("Text", _storageinfo, "Stono", true, DataSourceUpdateMode.OnPropertyChanged));
            cboStotype.DataBindings.Add(new Binding("SelectedValue", _storageinfo, "Stotype", true, DataSourceUpdateMode.OnPropertyChanged));
            txtNote.DataBindings.Add(new Binding("Text", _storageinfo, "Note", true, DataSourceUpdateMode.OnPropertyChanged));
        }

        private void toolStripAdd_Click(object sender, EventArgs e)
        {
            _storageinfo = new Storageinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            SaveInfo();
        }

        private void SaveInfo(bool isAdd = true)
        {
            if (txtStoname.Text.Length == 0)
            {
                MessageBox.Show(@"姓名不能为空，请输入.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtStoname.Focus();
                return;
            }
            _storageinfoBLL.Edit(_storageinfo);

            MessageBox.Show(@"保存成功.", @"提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (isAdd) this.DialogResult = DialogResult.OK;
        }

        private void tsbSaveAdd_Click(object sender, EventArgs e)
        {
            SaveInfo(false);
            _storageinfo = new Storageinfo();
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();
        }

        private void tsmPre_Click(object sender, EventArgs e)
        {
            PreviewRecord(_storageinfo.Id - 1, 0);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            PreviewRecord(_storageinfo.Id + 1, 1);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        private void PreviewRecord(long id, int type)
        {
            bool isLast = false;
            var obj = _storageinfoBLL.PreRecord(id, type, ref isLast);
            _storageinfo = obj;
            ControlUtil.ClearControlBind(superTabControl1);
            BindFormContro();

            if (isLast) MessageBox.Show(string.Format("当前已是{0}一条记录", type.Equals(0) ? "第" : "最后"));
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtStoname_Leave(object sender, EventArgs e)
        {
            txtStopym.Text = ChineseToSpell.GetChineseSpell(txtStoname.Text);
        }

        private void btnStotype_Click(object sender, EventArgs e)
        {
            CreateFormView.BindDicForm(cboStotype, SysDicEnum.仓库类别, isSelectRoot: false);
            _storageinfo.Stotype = Normal.ToInt(cboStotype.Tag);
        }

    }
}
